//app.js
var router = require('utils/router.js')
var approuter = router.router
App({
  onLaunch: (options) => {
    console.log("[onLaunch] 场景值:", options.scene)
    if (options.scene == 1007) {
      console.log("--------++++++++")
      console.log(options)
    }
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var sessionKey = wx.getStorageSync('LoginSessionKey');
        if(sessionKey != "" && sessionKey != null){
          var header = { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'JSESSIONID=' + sessionKey }
        }else{
          var header = { 'content-type': 'application/x-www-form-urlencoded' }
        }
        wx.request({
          url: approuter.userLogin,
          data: { code: res.code },
          method: 'GET',
          header:header,
          success: res => {
            console.log(res)
            if (res.data.Status == 'SUCCESS' && res.data.result.Status == 'SUCCESS') {
              wx.setStorageSync('LoginSessionKey', res.data.result.SessionKey)
              console.log(res.data.result.SessionKey)
            }
          }
        })
      },
      fail: res => {
        console.log('网络错误')
      }
    })
    /**
    wx.checkSession({
      success: function () {
        console.log("HHHHHHH")
        wx.redirectTo({
          url: 'pages/my/my',
        })
      },
      // 从新登陆
      fail: function () {
        //登录态过期
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            var sessionKey = wx.getStorageSync('LoginSessionKey');
            if(!sessionKey){
                sessionKey = null;
            }
            wx.request({
              url: 'http://localhost:8080/yuyueke/user/login',
              data: { code: res.code, sessionkey: sessionKey },
              method: 'GET',
              success: res => {
                if (res.Status == 'SUCCESS' && res.result.Status == 'SUCCESS') {
                  wx.setStorageSync('LoginSessionKey', res.result.SessionKey)
                  wx.redirectTo({
                    url: '../my/my',
                  })
                }
              }
            })
          },
          fail: res => {
            console.log('网络错误')
          }
        })
      } 
    }),
    */
  },
 
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      console.log(this.globalData.userInfo)
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.getUserInfo({
        withCredentials: false,
        success: function (res) {
          that.globalData.userInfo = res.userInfo
          console.log(res.userInfo)
          typeof cb == "function" && cb(that.globalData.userInfo)
        }
      })
    }
  },

  globalData:{
    userInfo:null,
    switchtomy:false,
  }
})